package com.atguigu.tingshu.search.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author atguigu-mqx
 * @ClassName ThreadPoolExecutorConfig
 * @description: TODO
 * @date 2024年09月11日
 * @version: 1.0
 */
//@Configuration
@Component
public class ThreadPoolExecutorConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(){
        //  获取CPU 核数
        int corePoolSize = Runtime.getRuntime().availableProcessors();

        return new ThreadPoolExecutor(
                corePoolSize, //  核心线程数 IO:2n CPU:n+1;  n: CPU核数
                10, //  最大线程数
                3l, // 空闲线程存活时间
                TimeUnit.SECONDS, // 时间单位
                new ArrayBlockingQueue<>(3), // 阻塞队列
                Executors.defaultThreadFactory(), // 默认的线程工厂
                new ThreadPoolExecutor.AbortPolicy() // 拒绝策略: jdk 自带4 ，还可以自定义拒绝策略.
        );
    }




}
